package com.earthchen.core.support.quartz.service.impl;

import com.earthchen.core.support.quartz.SchedulerManager;
import com.earthchen.core.support.quartz.domain.TaskScheduled;
import com.earthchen.core.support.quartz.service.SchedulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 定时任务服务实现类
 *
 * @author earthchen
 * @date 2018/9/1
 **/
@Service
@ConditionalOnProperty(name = "quartz.open", havingValue = "true")
public class SchedulerServiceImpl implements SchedulerService, ApplicationContextAware {

    @Autowired
    private SchedulerManager schedulerManager;

    protected ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取所有作业
     *
     * @return
     */
    @Override
    public List<TaskScheduled> getAllTaskDetail() {
        return schedulerManager.getAllJobDetail();
    }

    /**
     * 执行作业
     *
     * @param taskScheduler
     */
    @Override
    public void execTask(TaskScheduled taskScheduler) {
        schedulerManager.runJob(taskScheduler);
    }

    /**
     * 恢复作业
     *
     * @param taskScheduled
     */
    @Override
    public void openTask(TaskScheduled taskScheduled) {
        schedulerManager.resumeJob(taskScheduled);
    }

    /**
     * 暂停作业
     *
     * @param taskScheduled
     */
    @Override
    public void closeTask(TaskScheduled taskScheduled) {
        schedulerManager.stopJob(taskScheduled);
    }

    /**
     * 删除作业
     *
     * @param taskScheduled
     */
    @Override
    public void delTask(TaskScheduled taskScheduled) {
        schedulerManager.delJob(taskScheduled);
    }


    /**
     * 修改任务
     *
     * @param taskScheduled
     */
    @Override
    public void updateTask(TaskScheduled taskScheduled) {
        schedulerManager.updateTask(taskScheduled);
    }
}
